import Vue from 'vue'
import VueRouter from 'vue-router'
// 登录
const Login = () => import('../views/login/login.vue')
// 首页
const Home = () => import('../views/home/home.vue')
// 欢迎页
const Welcome = () => import('../views/welcome/welcome.vue')
// 用户
const Users = () => import('../views/users/users.vue')
// 权限
const Rights = () => import('../views/rights/rights.vue')
// 角色
const Roles = () => import('../views/rights/roles.vue')
// 商品分类
const Cate = () => import('../views/goods/cate.vue')
// 商品参数
const Params = () => import('../views/goods/params.vue')
// 商品列表
const List = () => import('../views/goods/list.vue')
// 添加商品
const AddGood = () => import('../views/goods/AddGood.vue')
// 订单管理
const Order = () => import('../views/order/order.vue')
// 数据报表
const Report = () => import('../views/report/report.vue')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/users',
        component: Users
      },
      {
        path: '/rights',
        component: Rights
      },
      {
        path: '/roles',
        component: Roles
      },
      {
        path: '/categories',
        component: Cate
      },
      {
        path: '/params',
        component: Params
      },
      {
        path: '/goods',
        component: List
      },
      {
        path: '/goods/add',
        component: AddGood
      },
      {
        path: '/orders',
        component: Order
      },
      {
        path: '/reports',
        component: Report
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 前置路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  if (!sessionStorage.getItem('token')) return next('/login')
  next()
})

export default router
